<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Ferry_Ticket_Type extends ORM
{
    /**
     * @function 获取门票类型
     * @param      $id
     * @param bool $column
     * @return mixed
     */
    public static function get_info($id, $column = false)
    {
        if ($column) {
            return DB::select($column)
                ->from('ferry_ticket_type')
                ->where('id', '=', $id)
                ->execute()
                ->get($column, '');
        }

        return DB::select()
            ->where('ferry_ticket_type')
            ->where('id', '=', $id)
            ->execute()
            ->current();
    }

    /**
     * 批量获取门票类型名称数组
     * @param $ticket_type_ids
     * @return array
     */
    public static function get_ticket_type_names($ticket_type_ids)
    {
        if (!empty($ticket_type_ids)) {
            $ticket_type_ids = array_unique($ticket_type_ids);
            if (!empty($ticket_type_ids)) {
                return DB::select()
                    ->from('ferry_ticket_type')
                    ->where('id', 'IN', $ticket_type_ids)
                    ->execute()
                    ->as_array('id', 'kindname');
            }
        }
        return array();
    }

    /**
     * 保存门票类型
     * @param $ferry_id
     * @param $name
     * @return array
     */
    public static function add_ticket_type($ferry_id, $name)
    {
        try {
            $orm = ORM::factory('ferry_ticket_type')
                ->where("kindname", '=', $name)
                ->where('ferryid', '=', $ferry_id)
                ->find();
            if (!empty($orm->id)) {
                throw new Exception('类型名称重复');
            }
            $orm           = ORM::factory('ferry_ticket_type');
            $orm->kindname = $name;
            $orm->ferryid   = $ferry_id;
            $orm->save();
            if ($orm->saved()) {
                $orm->reload();
                return array('status' => true, 'data' => $orm->as_array());
            } else {
                throw new Exception('保存失败');
            }
        } catch (Exception $e) {
            return array('status' => false, 'msg' => $e->getMessage());
        }
    }

    /**
     * 门票类型名
     * @param $ticket_type_id
     * @return mixed
     */
    public static function get_type_name($ticket_type_id)
    {
        return DB::select('kindname')
            ->from('ferry_ticket_type')
            ->where('id', '=', $ticket_type_id)
            ->execute()
            ->get('kindname');
    }
}